package mylab.utils.task;


import mylab.utils.common.ThreadUtil;

/**
 * File Name             :  AbstractTaskService
 *
 * @author :  sylar
 * Create                :  2019-11-22
 */
public abstract class AbstractTaskService<T> implements TaskContext<T> {

    protected Task<T> mainTask;
    protected TaskCallback<T> callback;

    public void start(TaskCallback<T> callback, Object... args) {
        this.callback = callback;

        ThreadUtil.postRunnable(() -> {
            mainTask = createRoadmap();
            try {
                callback.onSuccess(mainTask.start(args));
            } catch (TaskException e) {
                callback.onFailure(e);
            }
        });
    }

    protected abstract Task<T> createRoadmap();

    @Override
    public TaskCallback<T> getTaskCallback() {
        return callback;
    }
}
